﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.FrmKP
{
    /// <summary>
    /// 回写/修正税控号码-手工号码-拆单
    /// </summary>
    public partial class FrmReturn_SK : Form
    {
        private Model.SK_SalebillVouch _sk;
        private List<Model.SK_SalebillVouchs_View> _l_sks;
        public bool bSuc = false;

        /// <summary>
        /// 部分开票的情况
        /// </summary>
        /// <param name="fpdm"></param>
        /// <param name="fphm"></param>
        public FrmReturn_SK(Model.SK_SalebillVouch sk, List<Model.SK_SalebillVouchs_View> l_sks,
            string fpdm, string fphm, bool bNeedAuto)
        {
            InitializeComponent();
            this.Icon = YTSK.Properties.Resources.Fly;
            _sk = sk;
            _l_sks = l_sks;
            SetFPHM(fpdm, fphm, bNeedAuto);
        }

        private void SetFPHM(string fpdm, string fphm, bool bNeedAuto)
        {
            // string fpdmsave = "";
            // string fphmsave = "";
            // if (bNeedAuto)
            // {
            //     if (fphm.Length == 0)
            //     {
            //         return;
            //     }
            //
            //     var url =
            //         $"{ServerConfig.ServerAddress}/api/oms/SKSalebillVouch/GetNextVouchCode?token={ServerConfig.GetToken()}";
            //     var dic = new Dictionary<string, object>();
            //     dic.Add("cBillType", _sk.cBillType);
            //     dic.Add("fpdm", fpdm);
            //     dic.Add("fphm", fphm);
            //
            //
            //     var s = Common.handleHttp.Post(dic, url);
            //     var result = Common.handleJSON.DeserializeResult(s);
            //     if (result.result != 1)
            //     {
            //         return;
            //     }
            //
            //     fpdmsave = result.value["fpdm"].ObjToString();
            //     fphmsave = result.value["fphm"].ObjToString();
            // }

            if (fpdm.ObjToString().Length > 0)
            {
                this.tbfpdm.Text = fpdm;
            }

            // if (fpdmsave.ObjToString().Length > 0)
            // {
            //     this.tbfpdm.Text = fpdmsave;
            // }

            // if (fpdm == fpdmsave & fpdm.ObjToString().Length > 0)
            // {
            //     this.tbfpdm.ReadOnly = true;
            // }
            // else
            // {
            //     this.tbfpdm.ReadOnly = false;
            // }

            if (fphm.ObjToString().Length > 0)
            {
                this.tbfphm.Text = fphm;
            }

            // if (fphmsave.ObjToString().Length > 0)
            // {
            //     this.tbfphm.Text = fphmsave;
            // }
            //
            // if (fphm == fphmsave & fphm.ObjToString().Length > 0)
            // {
            //     this.tbfphm.ReadOnly = true;
            // }
            // else
            // {
            //     this.tbfphm.ReadOnly = false;
            // }
        }


        #region 回写发票

        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                string url =
                    $"{ServerConfig.ServerAddress}/api/oms/SKSalebillVouch/ReturnVouch?token={ServerConfig.GetToken()}";

                _sk.fpdm = this.tbfpdm.Text.Trim();
                _sk.fphm = this.tbfphm.Text.Trim();
                var err = CheckFP(_sk.fpdm, _sk.fphm);
                if (err.Length > 0)
                {
                    MessageBox.Show(err);
                    return;
                }

                _sk.dDate = this.dateTimePicker1.Value.Date;

                var pos = new Dictionary<string, object>();
                pos.Add("main", _sk);
                pos.Add("detail", _l_sks);

                var s = Common.handleHttp.Post(pos, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1)
                {
                    MessageBox.Show("回写发票号出现错误" + result.resultdetail);
                    return;
                }

                this.bSuc = true;
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }

        #endregion

        #region 检查发票号码及发票代码的长度

        private string CheckFP(string fpdm, string fphm)
        {
            var err = "";
            //统一为8位
            if (fphm.Length != 8)
            {
                err += string.Format("发票号码的长度必须为8位，[{0}]的长度[{1}]位不正确", fphm, fphm.Length);
            }

            //电子发票12位，普通发票和专用发票10位
            if (fpdm.Length != 10 && fpdm.Length != 12)
            {
                err += string.Format("发票代码的长度必须为10或12位，[{0}]的长度[{1}]位不正确", fpdm, fpdm.Length);
            }

            return err;
        }

        #endregion


        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}